// Runtime.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <ShellAPI.h>
#include "runtime.h"
#include <vld.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

//#include "engine.h"
//#include "render/rendersystem.h"
#include "engine.h"
#include "entity/game.h"
#include "../game/leogame.h"
//#include "json/json.h"

HWND hWnd=0;


void init()
{
	fuck_you_all();
	new game_t();
	GAME->init(hWnd);
}

bool update()
{
	bool e=GAME->update();

	if (!e)
		GAME->render();

	return e;
}

void exit()
{
	GAME->exit();
	delete GAME;
}


HWND CreateMyWindow(LPSTR strWindowName, int width, int height, HINSTANCE hInstance, bool fullscreen)
{
   
    HWND     hWnd;
    WNDCLASS wc;
 
    DWORD dwExStyle;
    DWORD dwStyle;
 
    RECT rWindow;
 
    memset(&wc, 0, sizeof(WNDCLASS));
    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_3DSHADOW + 1);
    wc.lpszClassName = "Leo";
 
    RegisterClass(&wc);
 
    hInst = hInstance;
 
    rWindow.left   = 0;
    rWindow.right  = width;
    rWindow.top    = 0;
    rWindow.bottom = height;//+15;
     
    if(fullscreen) {
         
        dwExStyle = WS_EX_APPWINDOW;
        dwStyle   = WS_POPUP;
//        ShowCursor(FALSE);
		rWindow.right=GetSystemMetrics(SM_CXSCREEN);
		rWindow.bottom=GetSystemMetrics(SM_CYSCREEN);
         
    }        
     
    else {
     
        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        dwStyle =  WS_OVERLAPPED|WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
		AdjustWindowRectEx(&rWindow, dwStyle, FALSE, dwExStyle);

    }    
 
 
    hWnd = CreateWindowEx(dwExStyle, "Leo", strWindowName, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
                          0, 0, rWindow.right - rWindow.left, rWindow.bottom - rWindow.top, NULL, NULL, hInstance, NULL);
 
    if(!hWnd) return NULL;
 
    ShowWindow(hWnd, SW_SHOWMAXIMIZED);
    SetFocus(hWnd);
 
    return hWnd;
     
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	ZeroMemory( &msg, sizeof( msg ) );
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_RUNTIME, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);


	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
//	    ChangeDisplaySettings(0,0);
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RUNTIME));

	SetForegroundWindow(hWnd);						// Slightly Higher Priority

	init();
	// Main message loop:
	while(msg.message != WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			if (update())
			{
				ShowWindow(hWnd,SW_MINIMIZE);
				UpdateWindow(hWnd);
				SHELLEXECUTEINFO ShellInfo; // Name structure
				memset(&ShellInfo, 0, sizeof(ShellInfo)); // Set up memory block
				ShellInfo.cbSize = sizeof(ShellInfo); // Set up structure size
				ShellInfo.hwnd = hWnd; // Calling window handle
				ShellInfo.lpVerb = "open";; // Open the file with default program
				ShellInfo.lpFile = "LeoQuestionaire.exe";; // File to open
				ShellInfo.nShow = SW_NORMAL; // Open in normal window
				ShellInfo.fMask = SEE_MASK_NOCLOSEPROCESS; // Necessary if you want to wait for spawned process
				bool res = ShellExecuteEx(&ShellInfo); // Call to function
				if (res)
					WaitForSingleObject(ShellInfo.hProcess, INFINITE); // wait forever for process to finish

			    ShowWindow(hWnd, SW_RESTORE);
				UpdateWindow(hWnd);
			}
		}
	}

	exit();


//    ChangeDisplaySettings(0,0);
	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.style			= CS_CLASSDC;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RUNTIME));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= 0;//MAKEINTRESOURCE(IDC_RUNTIME);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
#include "core/log.h"
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   bool fullscreen=false;
	int w=1920,h=1080;
/*
	SHELLEXECUTEINFO info;
	info.cbSize=sizeof(SHELLEXECUTEINFO);
	info.
	ShellExecuteEx(info);
*/
   if (MessageBox(0,"FullScreent akarsz?","",MB_YESNO)==IDYES)
   {
	   fullscreen=true;
	   w=1280;
	   h=720;
   }

#if 1
hWnd=CreateMyWindow("Leo", w,h, hInstance, fullscreen);
#elif 1
   hWnd = CreateWindow(szWindowClass, "Leo", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 1280, 720, NULL, NULL, hInstance, NULL);
//   0, 0, 0,0, NULL, NULL, hInstance, NULL);
#else
	hWnd = CreateWindow( szWindowClass, szTitle,
	   //		WS_POPUP|WS_VISIBLE, 0, 0, 1280, 544,//GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
	   WS_POPUP|WS_VISIBLE, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
	   NULL, NULL, hInstance, NULL );
#endif

   if (!hWnd)
   {
		DWORD err=GetLastError();
      return FALSE;
   }

   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
#include "render/rendersystem.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_SIZE:
		{
			if (RSYS)
				RSYS->on_resize(true);
		}
		break;
	case WM_KEYDOWN:
		if (wParam==VK_ESCAPE)
		{
			PostQuitMessage(0);
//			SendMessage(hWnd,WM_QUIT,0,0);
		}
		break;;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
